<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
class Index extends Controller
{

  //登录
  public function index()
  {
       $data=Request::instance()->post();

       if(empty($data) || !isset($data['username']) || $data['username']==null || !isset($data['password']) || $data['password']==null)
       {
             $arr['code']=1;
             $arr['msg']='用户名或密码错误';
             $arr['data']=[];
             return json($arr);
       }
       $res=db::table('Admin')->where($data)->find();

       if(!$res){
             $arr['code']=1;
             $arr['msg']='用户名或密码错误';
             $arr['data']=[];
             return json($arr);
       }
             $arr['code']=0;
             $arr['msg']='ok';
             $arr['data']=[];
             return json($arr);
  }
  //3次免登录
  public function login()
  {
       $username=request()->post('name','');
       $password=request()->post('password','');

       if($username=='' || $password=='' || $username==null $password==null)
       {
             $arr['code']=1;
             $arr['msg']='用户名或密码错误';
             $arr['data']=[];
             return json($arr);
       }
        

  }


    public function show()
    {
          if(!$_GET){
          	    return json(['msg'=>'无此信息','code'=>1,'data'=>'','status'=>1]);
          }
          $page=$_GET['page'];
          $limit=$_GET['limit'];
          $data=Db::table('good')->paginate($limit,false,['page'=>$page]);

          if(!$data){
          	     return json(['msg'=>'查询是失败','code'=>1,'data'=>'','status'=>1]);
          } 
                 return json(['msg'=>'查询成功','code'=>0,'data'=>$data,'status'=>0]);
    }
    //删除
    public function del()
    {
         $id=Request::instance()->GET('id','');

         $where['id']=$id;
         $res=Db::table('good')->delete($id);

          if(!$res){
          	     return json(['msg'=>'删除失败','code'=>1,'data'=>'','status'=>1]);
          } 
                 return json(['msg'=>'删除成功','code'=>0,'data'=>$res,'status'=>0]);
    }
    //修改
    public function updat()
    {     $arr=input('get.');

          $data=model('Good')->updateGood($arr);
           if(!$data){
          	     return json(['msg'=>'修改失败','code'=>1,'data'=>'','status'=>1]);
          } 
                 return json(['msg'=>'修改成功','code'=>0,'data'=>$res,'status'=>0]);
    }
    //搜索
   public function search()
   {
       $search=input('get.');
       $page=input('get.page');
       $limit=input('get.limit');

       $res=model('Good')->getsearch($page,$limit,$search);
         if(!$res){
                 return json(['msg'=>'搜索失败','code'=>1,'data'=>'','status'=>1]);
          } 
                 return json(['msg'=>'搜索成功','code'=>0,'data'=>$res,'status'=>0]);

   }
}
